#include "xmodule_config.h"

#if MODULE_TOUCHPANEL_ENABLE && (TP_CHIP_TYPE == TP_CHIP_SPD2012INCELL)

#include <string.h>


// #include "em_tp_internal.h"

#include "app_util.h"

#include "log.h"

//#if TPL_FIRMWARE_UPGRADE_SUPPORTED
//#include TP_FIRMWARE_FILE
//#endif

#define APP_SLAVE_ADDR               0x53//normal slave addr
                                                                             
#include "spd2012incell/spd2010_i2c.c"
#include "spd2012incell/spd2010_read_point.c"
#include "spd2012incell/spd2010_mgd_parse.c"
#include "spd2012incell/spd2010_fw_update.c"

#define ABS(x)      ((x) ? (x) : -(x))

/******************************************************************
origin drv
******************************************************************/
bool em_touch_panel_mounting(uint8_t* p_chip_id, uint8_t* p_version)
{
//    TP_I2C_SLAVE_ADDR_SET( APP_SLAVE_ADDR );
//    Read_FW_Version();

    return true;
}

void em_touch_panel_update(void)
{
    uint16_t slide_dist;
    uint8_t u8_test;
    
    TP_I2C_SLAVE_ADDR_SET( APP_SLAVE_ADDR );
    TP_Read_Data();
    
    if ((Touch.touch_num  == 1) && Touch.up)
    {
        slide_dist = ABS(Touch.up_x - Touch.down_x)
                     + ABS(Touch.up_y - Touch.down_y);
        if (slide_dist > 200)
        {
            /* Load LCD Image */
            u8_test++;
            u8_test = u8_test%2;
            nrf_delay_ms(1000);
            LOG0("[TP]:slide move");
        }
    }
    else if ((Touch.touch_num  == 0x02)
             || (Touch.gesture  == 0x02))
    {
        /* 2 Finger or Palm */
        nrf_delay_ms(100);
        LOG0("[TP]:LCD SleepIn");
    }
    else if (Touch.gesture  == 0x04)
    {
        /* Double Tap */
        nrf_delay_ms(100);
        LOG0("[TP]:LCD SleepOut");
    }

    Touch.touch_num = 0x00;
    Touch.gesture = 0x00;
}


void em_touchpanel_enable(void)
{
    
//    tp_enable();
}

void em_touchpanel_disable(void)
{    
    
//    tp_disable();
}

#if TPL_FIRMWARE_UPGRADE_SUPPORTED // update 
void em_touch_panel_boot_update(uint32_t firmware_address, uint8_t *ppercent )
{
    TP_I2C_SLAVE_ADDR_SET( APP_SLAVE_ADDR );
    SPD2010_FW_Update();
    tp_hw_reset();
}
#endif

#endif // ! MODULE_TOUCHPANEL_ENABLE && (TP_CHIP_TYPE == TP_CHIP_CST816D)
